home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / docs / misc / ConcNews.lha / news / general.programming / comp.programming_2212_000028.msg < prev    next >
Encoding:
Text File  |  1994-11-27  |  2.0 KB  |  47 lines

  1. Newsgroups: comp.programming
  2. From: fred@genesis.demon.co.uk (Lawrence Kirby)
  3. Path: dd.chalmers.se!news.chalmers.se!sunic!pipex!demon!genesis.demon.co.uk!fred
  4. Subject: Re: Conservative Sorting/Presv Order of Orig??
  5. References: <ConxHx.KHv@acsu.buffalo.edu>
  6. Organization: none
  7. Reply-To: fred@genesis.demon.co.uk
  8. X-Newsreader: Demon Internet Simple News v1.27
  9. Lines: 33
  10. Date: Sun, 24 Apr 1994 15:26:17 +0000
  11. Message-ID: <767201177snz@genesis.demon.co.uk>
  12. Sender: usenet@demon.co.uk
  13.  
  14. In article <ConxHx.KHv@acsu.buffalo.edu>
  15.            cudmore@acsu.buffalo.edu "Robert H. Cudmore" writes:
  16.  
  17. >Hello all,
  18. >
  19. >        I am attempting to sort an array of elements A.  I have a Heap sort
  20. >up and running, the problem is this:  I need the elements with equal keys
  21. >to remain in their original order.  Heap sort with building(hiring) and 
  22. >Sorting(firing) of the array into a heap and then into a sorted array seems
  23. >to cause the destruction of the original order of all elements. 
  24.  
  25. Sorts which preserve the order of equal keys are described as 'stable' sorts.
  26.  
  27. >        Are there efficient O(nlogn) sorting routines that will preserve this
  28. >original order?  Quicksort seems to do the same destructive ordering of
  29. >the data where as a simple insertion sort does not.  I would like to stay
  30. >away from linear complexity sorts and would also like to know if I can't
  31. >stay away from them.
  32.  
  33. Heapsort and Quicksort are not stable. Mergesort is stable, guaranteed O(nlogn)
  34. in the worst case and is just about the fastest of the O(nlogn) sorts. However
  35. if you are sorting arrays rather than lists it requires extra workspace (e.g.
  36. an extra array of n pointers).
  37.  
  38. You can always make any sort stable by adding a secondary key for
  39. each element which you initialise in ascending order before starting the
  40. sort.
  41.  
  42. -- 
  43. -----------------------------------------
  44. Lawrence Kirby | fred@genesis.demon.co.uk
  45. Wilts, England | 70734.126@compuserve.com
  46. -----------------------------------------
  47.